﻿using System.Collections.Generic;
using Imps.Client.Pc;
using Imps.Client.Core;
using System.Windows.Forms;

namespace com.hetaoos.FetionSDK.Message
{
    public class ContactBase
    {
        private FetionSDK _frameworkWnd;
        private SendMultiSMSControl _sendSMS;
        private SendMultiIMControl _sendIM;

        public SendMultiIMControl SendIM
        {
            get { return _sendIM; }
        }

        public SendMultiSMSControl SendSMS
        {
            get { return _sendSMS; }
        }

        public ContactBase(FetionSDK framework)
        {
            _frameworkWnd = framework;
            _sendSMS = new SendMultiSMSControl(framework,this);
            _sendIM = new SendMultiIMControl(framework, this);

        }
        
        public List<ContactGroup> getContactGroup()
        {
            List<ContactGroup> list = new List<ContactGroup>();
            lock (_frameworkWnd.AccountManager.CurrentUser.ContactList.Groups.SyncRoot)
            {
                list.Clear();
                foreach (ContactGroup group in _frameworkWnd.AccountManager.CurrentUser.ContactList.Groups)
                {
                    if (group.Id != -1)
                    {
                        list.Add(group);
                    }
                }
            }
            return list;
        }

        public Contact getMyself()
        {
            return _frameworkWnd.AccountManager.CurrentUser.ContactList.FindOrCreateContact(_frameworkWnd.AccountManager.CurrentUser.Uri.Raw, new AsyncBizOperation());
        }

        public List<Contact> getContactList(ContactGroup group)
        {
            List<Contact> list = new List<Contact>();

            if (group == null || group.Id != -1)
            {
                return list;
            }
            lock (_frameworkWnd.AccountManager.CurrentUser.ContactList.Groups.SyncRoot)
            {
                List<Contact> listContacts = group.Contacts.ListContacts;
                foreach (Contact contact in listContacts)
                {
                    if (!(contact is Imps.Client.Core.ChatFriend))
                    {
                        list.Add(contact);
                    }
                }
            }

            return list;
        }

        public List<Contact> getAllContactList()
        {
            List<Contact> list = new List<Contact>();
            list.Add(getMyself());
            lock (_frameworkWnd.AccountManager.CurrentUser.ContactList.Groups.SyncRoot)
            {
                foreach (Contact contact in _frameworkWnd.AccountManager.CurrentUser.ContactList.Contacts)
                {
                    list.Add(contact);
                }
            }

            return list;
        }


        public Contact findContact(string sid)
        {
            if (sid.Length == 11)
            {
                return findContactByMobileNo(sid);
            }
            else
            {
                return findContactBySID(sid);
            }
        }
        /// <summary>
        /// 通过飞信号找用户
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        public Contact findContactBySID(string sid)
        {
            List<Contact> _listContacts = getAllContactList();
            for (int i = 0; i < _listContacts.Count; i++)
            {
                if (_listContacts[i].Uri.Id == sid)
                {
                    return _listContacts[i];
                }
            }
            return null;
        }
        /// <summary>
        /// 通过手机号找用户
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public Contact findContactByMobileNo(string mobile)
        {
            List<Contact> _listContacts = getAllContactList();
            for (int i = 0; i < _listContacts.Count; i++)
            {
                if (_listContacts[i].PersonalInfo.MobileNo == mobile)
                {
                    return _listContacts[i];
                }
            }
            return null;
        }

    }
}
